package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ArticleDTO;
import com.api.shopapi.entity.dto.TrainDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("Train")
public class TrainController extends BaseController{



    @Autowired
    @Qualifier("TrainServiceImplOne")
    private TrainService trainService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("TrainClassServiceImplOne")
    private TrainClassService trainClassService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String trainClassCode = jsonObject.getString("trainClassCode");
            String title =jsonObject.getString("title");
            String description=jsonObject.getString("description");
            String content =jsonObject.getString("content");
            String fileUrl1=jsonObject.getString("fileUrl1");
            String fileUrl2=jsonObject.getString("fileUrl2");
            String fileUrl3=jsonObject.getString("fileUrl3");

            String videoUrl = jsonObject.getString("videoUrl");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff =staffService.getByStaffCode(staffCode);

            TrainClass trainClass = trainClassService.getByTrainClassCode(trainClassCode);

            Train train =new Train();
            train.setTrainCode(UUID.randomUUID().toString());
            train.setTrainClassCode(trainClassCode);
            train.setTrainClassName(trainClass.getName());
            train.setTitle(title);
            train.setDescription(description);
            train.setContent(content);
            train.setCreateTime(new Date());
            train.setStatus(1);
            train.setGovernmentCode(staff.getGovernmentCode());
            train.setFileUrl1(fileUrl1);
            train.setFileUrl2(fileUrl2);
            train.setFileUrl3(fileUrl3);
            train.setVideoUrl(videoUrl);
            train.setAskAnswerNameCode(askAnswerNameCode);
            trainService.insert(train);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Article/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String title =jsonObject.getString("title");
            String description=jsonObject.getString("description");
            String content =jsonObject.getString("content");
            String trainCode=jsonObject.getString("trainCode");
            String trainClassCode = jsonObject.getString("trainClassCode");
            int status = jsonObject.getIntValue("status");
            String fileUrl1=jsonObject.getString("fileUrl1");
            String fileUrl2=jsonObject.getString("fileUrl2");
            String fileUrl3=jsonObject.getString("fileUrl3");

            String videoUrl = jsonObject.getString("videoUrl");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Train train = trainService.getByTrainCode(trainCode);

            TrainClass trainClass = trainClassService.getByTrainClassCode(trainClassCode);

            train.setTrainClassCode(trainClassCode);
            train.setTrainClassName(trainClass.getName());
            train.setTitle(title);
            train.setDescription(description);
            train.setContent(content);
            train.setStatus(status);
            train.setFileUrl1(fileUrl1);
            train.setFileUrl2(fileUrl2);
            train.setFileUrl3(fileUrl3);
            train.setVideoUrl(videoUrl);
            train.setAskAnswerNameCode(askAnswerNameCode);
            trainService.update(train);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Article/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //预览
    @RequestMapping(value = "Preview",method = RequestMethod.POST)
    public MyResult Preview(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String trainCode=jsonObject.getString("trainCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Preview");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Train train =trainService.getByTrainCode(trainCode);

            return success(train);
        } catch (Exception e) {
            insertError("/Article/Preview", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //预览
    @RequestMapping(value = "Get",method = RequestMethod.POST)
    public MyResult Get(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String trainCode=jsonObject.getString("trainCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Article/Preview");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Train train =trainService.getByTrainCode(trainCode);

            return success(train);
        } catch (Exception e) {
            insertError("/Article/Preview", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String title = jsonObject.getString("title");
            int status = jsonObject.getIntValue("status");

            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime=endTime+" 23:59:59";

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff =staffService.getByStaffCode(staffCode);

            PageInfo<Train> articlePageInfo =
                    trainService.getPageList(pageIndex,pageSize,staff.getGovernmentCode(),title,status,startTime,endTime);

            return success(articlePageInfo);
        } catch (Exception e) {
            insertError("/Article/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPageApp",method = RequestMethod.POST)
    public MyResult GovPageApp(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Article/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            PageInfo<Train> articlePageInfo =
                    trainService.pageByGovCode(pageIndex,pageSize,user.getGovernmentCode());

            return success(articlePageInfo);
        } catch (Exception e) {
            insertError("/Article/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //下架
    @RequestMapping(value = "Revocation",method = RequestMethod.POST)
    public MyResult Revocation(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String trainCode=jsonObject.getString("trainCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Revocation");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Train train = trainService.getByTrainCode(trainCode);
            train.setStatus(2);
            trainService.update(train);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/Article/Revocation", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
